<?php
namespace Admin\Controller;
use Admin\Model\PriceModel;
use Admin\Model\StyleModel;
use Common\Model\CommonModel;
use Think\Controller;

class DesignerController extends BaseController{

    public function index(){
        //主页
        $designerModel=M('designer');
        //var_dump($designerModel);exit;
        $condition['name'] = array('like',"%".$_GET['name']."%");
        unset($_SESSION['like']['designer']);
        $_SESSION['like']['designer']=$_GET['name'];
        $count = $designerModel->where($condition)->count();
        $page= new \Think\Page($count,5);
        $page->setConfig('next','下一页');
        $page->setConfig('prev','上一页');
        //显示还有多少条
        $page->setConfig('theme','%HEADER% %FIRST% %UP_PAGE% %LINK_PAGE% %DOWN_PAGE% %END%');
        $show = $page->show();
        $list=$designerModel->where($condition)->order(array('create'=>'desc'))->limit($page->firstRow.','.$page->listRows)->select();
        $list=CommonModel::photo1($list);
        $styleModel=new StyleModel();
        $list=$styleModel->getStyle($list);
        //var_dump($list);exit;
        $this->assign('page',$show);
        $this->assign('list',$list);
        $this->display();
    }


    //修改页面
    public function update(){
        $designerModel=D('designer');
        //$designerModel=new PriceModel();
        $da['id']=$_GET['id'];
        //找到原数据
        $list=$designerModel->finded($da);
        $list=CommonModel::photo2($list);
       //var_dump($list);exit;
        $stylesModel=new StyleModel();
        $styles=$stylesModel->getAll();
        $this->assign('styles',$styles);
        $this->assign('list',$list);
        $this->display();
    }
    //执行修改
    public function doUpdate(){
        $designerModel=D('designer');
        $data['name']=$_POST['name'];
        $data['style_id']=$_POST['style_id'];
        $data['idea']=$_POST['idea'];
        $id=$_POST['id'];
        $where='id='.$id;
       // echo $id;exit;
        $list=$designerModel->finded($where);
        /* echo(json_encode($list));
         exit;*/
        if(!$_FILES['pic']['error']==4){
            $data['pic']=$this->upload();
        }
        $data['update']=time();


        //过滤重复内容
        $data=CommonModel::filter($data,$list);
        // echo json_encode($data);
        // exit;
        $res=$designerModel->update($where,$data);
        if($res){
            $this->success('修改成功',U('index'));
        }else{
            $this->error('修改失败');
        }
    }
    //删除
    public function del(){
        $designerModel=D('designer');
        $id=$_GET['id'];
        $res= $designerModel->deleted('id='.$id);
        if($res){
            $this->success('删除成功');
        }else{
            $this->error('删除失败');
        }
    }

    //添加设计师
    public function create(){

        if(empty($_POST)){
            $stylesModel=new StyleModel();
            $styles=$stylesModel->getAll();
            $this->assign('styles',$styles);
            $this->display();
            exit;
        }

        if(empty($_POST['name'])){
            $this->error('作者不能为空');
        }
        $data['name']=$_POST['name'];
        if(empty($_POST['style_id'])){
            $this->error('风格不能为空');
        }
        $data['style_id']=$_POST['style_id'];
        $data['idea']=$_POST['idea'];
        if(!$_FILES['pic']['error']==4){
            $data['pic']=$this->upload();
        }
        $data['create']=time();
        $designerModel=D('designer');

        $res=$designerModel->create($data);

        if($res){
            $this->success('添加成功',U('index'));
        }else{
            $this->error('添加失败');
        }




    }







}